package team.archai.nexus.boot.platform.basics.endpoint;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.archai.nexus.boot.auth.entity.AuthUserInfo;
import team.archai.nexus.boot.auth.entity.JwtParam;
import team.archai.nexus.boot.auth.model.AuthenticatedInfo;
import team.archai.nexus.boot.auth.token.IAuth;
import team.archai.nexus.boot.auth.util.AuthUtil;
import team.archai.nexus.boot.config.AuthProperties;
import team.archai.nexus.boot.platform.core.granter.AuthParameter;
import team.archai.nexus.boot.platform.core.granter.ITokenGranter;
import team.archai.nexus.boot.tool.Tool;
import team.archai.nexus.boot.web.response.ResponseModel;
import team.archai.nexus.boot.web.response.ResultUtil;

/**
 * @author lishibin
 * @desc: 授权认证
 * @date 2023-05-19
 */

@RestController
@RequestMapping("/auth")
@Tag(name = "授权认证", description = "授权认证接口")
public class AuthEndpoint {

    @Autowired
    private ITokenGranter tokenGranter;

    @Autowired
    private AuthProperties authProp;

    /**
     * @program: AuthLogin
     * @description: 授权用户token
     * @author: lishibin
     * @create: 3:10 PM
     **/
    @GetMapping("/grant")
    @Operation(summary = "授权认证", description = "授权认证")
    ResponseModel<AuthenticatedInfo> grant(
            @Parameter(
                    name = "authKey",
                    description = "授权KEY,grantType为password时候 值为md5(time) 为key 对用户密码账户组合【组合方式为account_password】进行aes ecb 模式加密",
                    required = true
            )
            @RequestParam(required = false) String authKey,
            @Parameter(
                    description = "接口签名时间戳,格式 ddd,YYMMDDThhmmssSSS ZZ",
                    in = ParameterIn.HEADER,
                    required = true
            )
            @RequestHeader(name = "Archai-Nexus-Nest-Api-Date") String signDate

    ) {
        //通过username 和password 构造出jwtParam
        long timeout = authProp.getTimeout() * 1000;
        AuthParameter authParameter = new AuthParameter();
        authParameter.getArgs().set("authKey", authKey).set("signDate", signDate);
        AuthenticatedInfo authenticatedInfo = tokenGranter.grant(authParameter);
        authenticatedInfo.setExpire(authProp.getTimeout());
        AuthUserInfo authUserInfo = new AuthUserInfo();
        authUserInfo.setUserId(1L);
        JwtParam jwtParam = JwtParam.builder().credential(authenticatedInfo.getCredential()).subject(authenticatedInfo.getSubject()).exp(timeout)
                .initialRandom(Tool.uuid()).additional(authUserInfo).build();
        IAuth response = AuthUtil.login(jwtParam);
        authenticatedInfo = response.getPrincipal();
        authenticatedInfo.setToken(authenticatedInfo.getToken());
        authenticatedInfo.setExpire(authenticatedInfo.getExpire());
        //根据账户获取到用户信息
        return ResultUtil.success(authenticatedInfo, "Granted.");
    }

    @GetMapping("/user-info")
    @Operation(summary = "获取授权用户信息", description = "获取授权用户信息")
    ResponseModel<AuthUserInfo> grant() {
        return ResultUtil.success(AuthUtil.authUserInfo());
    }


}
